<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>
        <el-tree
          :data="departs"
          :default-expand-all="true"
        >
          <template #default="scope">
            <treeTools
              :tree-node="scope.data"
              :is-root="false"
              @delDepartment="getDepartments"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <addDept
        ref="dialogAddDept"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        @addDeptFinish="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/department'
export default {
  components: {
    treeTools, addDept
  },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      departs: [],
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      // const res = await getDepartments()
      // console.log(res)
      // this.departs = res.depts
      // console.log(depts)
      this.departs = this.listToTreeData(depts, '')
      // console.log(this.departs)
    },
    // listToTreeData(list, topPid) {
    //   list.forEach(element => {
    //     if (element.pid !== topPid) { // 孩子不为空，所以element就是代表孩子
    //       const parent = list.find(item => item.id === element.pid) // item就是父母
    //       if (parent) {
    //         parent.children = parent.children || []
    //         parent.children.push(element)
    //       }
    //     }
    //   })
    //   return list.filter(item => item.pid === topPid) // pid为空的则留下，不为空的则删除
    // }
    listToTreeData(list, topPid) {
      const arr = []
      list.forEach(element => {
        if (element.pid === topPid) { // 父母为空，所以element就是代表父母
          const children = this.listToTreeData(list, element.id) // 进行递归操作，以父元素的id为准，对所有的对象的pid进行匹配，相同则为下一级
          // console.log(children)
          if (children.length > 0) { // 有children则让children添加至element的children属性中
            element.children = children
          }
          arr.push(element) // 将符合条件的element添加至arr中
        }
      })
      return arr // 返回一个arr数组
    },
    addDepartment(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    editDepartment(nodeData) {
      this.$refs.dialogAddDept.getDepartmentDetail(this.nodeData.id)
      this.showDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
